package com.cole.gx095.controller;


import com.cole.gx095.Repository.AdmininfoRepository;
import com.cole.gx095.controller.utils.CommonRep;
import com.cole.gx095.controller.utils.MD5Utils;
import com.cole.gx095.controller.utils.RedisUtil;
import com.cole.gx095.entity.Admininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;

@RestController
@RequestMapping("/user")
@CrossOrigin
public class loginController {
    @Autowired
    AdmininfoRepository admininfoRepository;
    @PostMapping("/login")
    CommonRep login(@RequestBody Admininfo admininfo){
        Admininfo byAdminName = admininfoRepository.findByAdminName(admininfo.getAdminName());

        if(byAdminName == null){
            return CommonRep.error();
        }
        if(!byAdminName.getAdminPwd().equals(MD5Utils.string2MD5(admininfo.getAdminPwd()))){
            return CommonRep.error();
        }
        RedisUtil.mset("LoginName",byAdminName.getTrueName());
        System.out.println(RedisUtil.mget("LoginName"));
        return CommonRep.ok().data("token","admin");
    }
    @GetMapping("/info")
    CommonRep info(){
        return CommonRep.ok()
                .data("roles","[admin]")
                .data("name","admin")
                .data("avatar","https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
    }
    @PostMapping("/logout")
    CommonRep logout(){
        RedisUtil.mdel("LoginName");
        return CommonRep.ok();
    }
}
